package com.kun97.im.controller;

import com.kun97.im.dal.mapper.UserMessageMapper;
import com.kun97.im.model.UserMessage;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @BelongsProject: im
 * @BelongsPackage: com.kun97.im.controller
 * @Author: zaydanwu
 * @CreateTime: 2019-01-01 17:38
 * @Description:
 */
@RestController
@RequestMapping("/im/api/usermessage/")
public class UserMessageController {

    @Resource
    private UserMessageMapper userMessageMapper;

    @RequestMapping("insert")
    int insert(@RequestBody UserMessage record) {
        UserMessage userMessage = userMessageMapper.selectByUserName(record.getUsername());
        if (!Objects.isNull(userMessage)) {
            return 666;
        }
        return userMessageMapper.insert(record);
    }

    @RequestMapping("selectUser")
    UserMessage selectUser(@RequestParam("username") String username, @RequestParam("password") String password) {
        UserMessage select = userMessageMapper.select(username, password);
        select.setPassword(null);
        return select;
    }

    @RequestMapping("selectUserById")
    UserMessage selectUserById(@RequestParam("uid") Integer uid) {
        UserMessage select = userMessageMapper.selectByPrimaryKey(uid);
        select.setPassword(null);
        return select;
    }

    @RequestMapping("selectUserByUserName")
    UserMessage selectUserByUserName(@RequestParam("username") String username) {
        UserMessage select = userMessageMapper.selectByUserName(username);
        select.setPassword(null);
        return select;
    }

    @RequestMapping("updateByPrimaryKey")
    int updateByPrimaryKey(@RequestBody UserMessage record) {
        return userMessageMapper.updateByPrimaryKey(record);
    }
}
